/*
 * Copyright (C) 2009 Las Cumbres Observatory (lcogt.net)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.lcogt.tools.fitsviewer.server;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import javax.imageio.*;
import javax.servlet.ServletContext;

import org.lcogt.tools.fitsviewer.client.ZoomJpegService;

import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ZoomJpegServiceImpl extends RemoteServiceServlet implements ZoomJpegService {
	
	java.awt.Image javaImage;
	int zoom;
	int width;
	int height;
	String URLBase;
	String base;
	File outputFile2=null;
	File outputFile4=null;
	Properties prop;
	public String getZoomJpeg(String image, int zoom, int width,int height) {
		//Finds path to Jpgs on server
		if(prop == null)
		{
			prop = new Properties();
			ServletContext context = getServletContext();
			InputStream is = context.getResourceAsStream("FitsViewer.properties");
			try {
				prop.load(is);
				URLBase = prop.getProperty("urlOfJPGS");
				base = prop.getProperty("serverPathOfJPGS");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.zoom = zoom;
		this.width = width;
		this.height = height;
		/*
		 * Loads fits(jpg) Image
		 * Creates two output files for Zoom x2 and x4
		 * Creates zoomed images
		 */
		try {
				javaImage = ImageIO.read(new File(base+ image));
				java.awt.Image javaImage2 = javaImage.getScaledInstance(width*2, height*2, javaImage.SCALE_DEFAULT);
				outputFile2 = new File(base + "ZoomImages/zoom2.jpg");
				ImageIO.write(toBufferedImage(javaImage2,BufferedImage.TYPE_BYTE_GRAY),"JPG",outputFile2);
				java.awt.Image javaImage4 = javaImage.getScaledInstance(width*4, height*4, javaImage.SCALE_DEFAULT);
				outputFile4 = new File(base +  "ZoomImages/zoom4.jpg");
				ImageIO.write(toBufferedImage(javaImage4,BufferedImage.TYPE_BYTE_GRAY),"JPG",outputFile4);
				return URLBase + "ZoomImages/zoom";
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		

	}
	//Creates a Buffered Image from a java.awt Image
	 public static BufferedImage toBufferedImage(final java.awt.Image image, final int type) {  
		  
		   final BufferedImage buffImg = new BufferedImage(image.getWidth(null), image.getHeight(null), type);  
		   final Graphics2D g2 = buffImg.createGraphics();  
		   g2.drawImage(image, null, null);  
		   g2.dispose();  
		   return buffImg;  
	}  
}
